package com.bridge.common.sms;





import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;


public class SmsTokenManger {

    private static SmsTokenManger manger;
    private List<SmsToken> smsTokenList = new ArrayList<SmsToken>();

    private SmsTokenManger() {


    }

    public static SmsTokenManger getInstance() {

        synchronized (SmsTokenManger.class) {
            if (manger == null) {
                manger = new SmsTokenManger();

                long halfTimeOut = 10 * 1000;    // Token最小过期时间的一半时间作为任务运行的间隔时间
                new Timer("smsTokenManager", true).schedule(new TimerTask() {
                                                                public void run() {
                                                                    manger.removeTimeOutToken();
                                                                }
                                                            },
                        halfTimeOut,
                        halfTimeOut);
            }


        }


        return manger;


    }

    public void updateExpiration(String tokenId, int second) {


        for (SmsToken token : smsTokenList) {

            if (token.getToken().equals(tokenId)) {

                token.setExpirationTime(System.currentTimeMillis() + second);
                return;

            }
        }


    }


    public SmsToken createToken(String code, String phone, int secondsOfTimeOut) {


        SmsToken token = new SmsToken(createRandTokenStr(), code, phone, System.currentTimeMillis() + (secondsOfTimeOut * 1000));


        smsTokenList.add(token);


        return token;


    }


    private String createRandTokenStr() {

        String token = IdGen.uuid();


        return token;

    }


    public void removeTimeOutToken() {


        if (smsTokenList == null || smsTokenList.size() == 0) {
            return;
        }

        List<SmsToken> timeOutTokens = new ArrayList<SmsToken>();
        long currentTime = System.currentTimeMillis();
        // find and save all time out tokens

        for (SmsToken token : smsTokenList) {
            if (token.getExpirationTime() <= currentTime) {
                timeOutTokens.add(token);
            }
        }


        smsTokenList.removeAll(timeOutTokens);



       /* // remove all time out tokens
        for (SmsToken token : timeOutTokens) {
            tokenInCache.remove(token);
        }*/

    }


    public synchronized boolean validateToken(String tokenId) {

        SmsToken smsToken = new SmsToken(tokenId);


        for (SmsToken token : smsTokenList) {
            if (token.getToken().equals(tokenId))
                return true;

        }
        return false;


    }

    public SmsToken getToken(String tokenId) {
        for (SmsToken token : smsTokenList) {
            if (token.getToken().equals(tokenId))
                return token;

        }

        return null;
    }

    public void removeToken(String tokenId) {

        if (smsTokenList == null)
            return;

        SmsToken token = getToken(tokenId);

        if (token == null)
            return;


        smsTokenList.remove(token);


    }


}
